﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Tenebrous_Manor
{
    class Enemy // need to center to tile
    {
        Texture2D texture;
        int shadeX;
        int shadeY;
        Rectangle sprite = new Rectangle(0, 0, 85, 110);
        Random rand = new Random();
        bool spawned;
        Navigation.Room current;
        Facing look;

        public Enemy(Texture2D texture, int sx, int sy)
        {
            this.texture = texture;
            this.shadeX = sx;
            this.shadeY = sy;
            spawned = false;
            current = Navigation.Room.MAIN_ENTRANCE;
        }

        public void spawnShade(bool following, int px, int py)
        {
            if (!following)
            {
                bool goodPoint = false;
                while (!goodPoint)
                {
                    shadeX = rand.Next(10);
                    shadeY = rand.Next(10);
                    float radius = (float)Math.Sqrt(Math.Pow(shadeX - px, 2) + Math.Pow(shadeY - py, 2));
                    if (radius > 3)
                    {
                        goodPoint = true;
                    }
                }
                spawned = true;
            }
            else
            {
                shadeX = px;
                shadeY = py;
                spawned = true;
            }
        }

        public void shadeDespawn()
        {
            spawned = false;
            shadeX = -5;
            shadeY = -5;
        }

        
        /*public bool isShadeSeen(Facing dir, int px, int py) // find functions for edges after demo
        {
            float radius = (float)Math.Sqrt(Math.Pow(shadeX-px, 2) + Math.Pow(shadeY-py, 2));
            float angle = (float)Math.Acos((shadeX-px)/radius);
            if (shadeY-py > 0) angle = (float)(2*Math.PI - angle);

            if (radius <= 3) // check max range
            {
                // check between correct angles
                if (dir == Facing.UP && angle >= Math.PI/3 && angle <= 2*Math.PI/3)
                {
                    return true;
                }
                else if (dir == Facing.LEFT && angle >= 5*Math.PI/6 && angle <= 7*Math.PI/6)
                {
                    return true;
                }
                else if (dir == Facing.DOWN && angle >= 4*Math.PI/3 && angle <= 5*Math.PI/3)
                {
                    return true;
                }
                else if (dir == Facing.RIGHT && ((angle >= 0 && angle <= Math.PI/6)) || (angle >= 11*Math.PI/6 && angle <= 2*Math.PI))
                {
                    return true;
                }
                else
                {
                    //Facing Error
                    return false;
                }
            }
            else return false;
        }*/

        public void move(int px, int py)
        {
            if (Math.Abs(shadeX - px) > Math.Abs(shadeY - py))
            {
                if (shadeX > px)
                {
                    shadeX -= 1;
                    look = Facing.LEFT;
                }
                else
                {
                    shadeX += 1;
                    look = Facing.RIGHT;
                }
            }
            else if (Math.Abs(shadeX - px) < Math.Abs(shadeY - py))
            {
                if (shadeY < py)
                {
                    shadeY += 1;
                    look = Facing.DOWN;
                }
                else
                {
                    shadeY -= 1;
                    look = Facing.UP;
                }
            }
            else
            {
                if (shadeX == px && shadeY == py)
                {
                    // GAMEOVER
                }
                else if (shadeX > px)
                {
                    shadeX -= 1;
                    look = Facing.LEFT;
                }
                else
                {
                    shadeX += 1;
                    look = Facing.RIGHT;
                }
            }

            switch (look)
            {
                case Facing.DOWN:
                    sprite.Y = 110 * 0;
                    break;
                case Facing.UP:
                    sprite.Y = 110 * 1;
                    break;
                case Facing.RIGHT:
                    sprite.Y = 110 * 2;
                    break;
                case Facing.LEFT:
                    sprite.Y = 110 * 3;
                    break;
            }
        }

        public void setRoom(Navigation.Room newRoom)
        {
            this.current = newRoom;
        }

        public Navigation.Room getRoom()
        {
            return this.current;
        }

        public bool isSpawned()
        {
            return this.spawned;
        }

        public int getShadeX()
        {
            return this.shadeX;
        }

        public int getShadeY()
        {
            return this.shadeY;
        }

        public void setTexture(Texture2D shadeText)
        {
            this.texture = shadeText;
        }

        public void newForm(int form)
        {
            this.sprite.X = 85 * form;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, new Vector2(shadeX*85, shadeY*85 - 25), sprite, Color.White);
        }
    }
}
